#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/10/23  20:51
# @Author: 余浪人
# @email: yulangren520@gmail.com
from random import sample
from datetime import datetime
from flask import current_app, request
from flask_caching import Cache


class MyCache(Cache):
    def cache_with_param(self,timeout=None, key_prefix='view/%s', unless=None):
        def key_prefix_func():
            with current_app.app_context():
                cache_key = key_prefix % request.url if '%s' in key_prefix else key_prefix
            return cache_key

        return cache.cached(timeout=timeout, key_prefix=key_prefix_func, unless=unless)


cache = MyCache(with_jinja2_ext=False)


class DictToObject:
    def __init__(self, **entries):
        self.__dict__.update(entries)


@cache.cached(timeout=(24 * (60 * 60)) * 30, key_prefix='sys_cache')
def sys_cache():
    from apps.models.system_model import TL_System_Model
    sys = TL_System_Model.query.first()
    return sys


@cache.cached(timeout=30 * (24 * (60 * 60)), key_prefix='current_theme')
def current_theme():
    from apps.models.synthesize_model import TL_ThemePush_Model
    theme = TL_ThemePush_Model.query.filter_by(is_current=1).first()
    if theme: return theme.identifier
    return ''

@cache.cached(timeout=(24 * (60 * 60)) * 30, key_prefix='redis_cache')
def redis_conf():
    from apps.models.system_model import TL_Redis_Model
    r_conf = TL_Redis_Model.query.filter().first()
    return r_conf

@cache.cached(timeout=(24 * (60 * 60)) * 30, key_prefix='mysql_cache')
def mysql_conf():
    try:
        from apps.models.system_model import TL_MySql_Model
        mysql_conf = TL_MySql_Model.query.filter().first()
        return mysql_conf
    except:
        return


def column_cache():
    from apps.webapi.webpublic.public import nav_list
    from apps.models.content_model import TL_Column_model,TL_Content_Model
    columnList = TL_Column_model.query.order_by(TL_Column_model.column_order.desc()).order_by(TL_Column_model.id.asc()).filter(
        TL_Column_model.status == 1, TL_Content_Model.update_time < datetime.now()).limit(1000).all()
    return nav_list(columnList)


# @cache.cached(timeout=12 * (60 * 60), key_prefix='notice_cache')
def notice_cache():
    from apps.models.system_model import TL_Notice_Model
    notice = TL_Notice_Model.query.order_by(TL_Notice_Model.id.desc()).filter(TL_Notice_Model.status == 1).limit(1000).all()
    return notice

@cache.cached(timeout=24 * (60 * 60), key_prefix='links_cache')
def links_cache():
    from apps.models.system_model import TL_Blogroll_Model
    links = TL_Blogroll_Model.query.order_by(TL_Blogroll_Model.link_sort.desc()).filter(TL_Blogroll_Model.status == 1, TL_Blogroll_Model.link_end_date >= datetime.now()).limit(1000).all()
    return links

# @cache.cached(timeout=60 * 60, key_prefix='randomReferrals_cache')
def random_content():
    from apps.models.content_model import TL_Content_Model
    con = TL_Content_Model.query.filter(TL_Content_Model.status == 1, TL_Content_Model.update_time < datetime.now()).limit(1000)

    random_document = sample(con.all(), 20 if con.count() >= 20 else con.count())
    return random_document

# @cache.cached(timeout=60 * 60, key_prefix='recommend_cache')
def rec_content():
    from apps.models.content_model import TL_Content_Model
    con_recommend = TL_Content_Model.query.order_by(TL_Content_Model.id.desc()).filter(TL_Content_Model.status == 1, TL_Content_Model.is_recommend == 1,TL_Content_Model.update_time < datetime.now()).limit(1000).all()
    return con_recommend

@cache.cached(timeout=60 * 60, key_prefix='hot_cache')
def hot_tags():
    from apps.models.content_model import TL_Tags_Model
    tags = TL_Tags_Model.query.order_by(TL_Tags_Model.tag_click.desc()).limit(1000).all()
    return tags

# @cache.cached(timeout=60 * 60, key_prefix='roll_cache')
def roll_text():
    from apps.models.content_model import TL_Content_Model
    roll_text = TL_Content_Model.query.order_by(TL_Content_Model.id.desc()).filter(TL_Content_Model.status == 1, TL_Content_Model.is_roll_text == 1, TL_Content_Model.update_time < datetime.now()).limit(1000).all()
    return roll_text

# @cache.cached(timeout=60 * 60, key_prefix='carousel_cache')
def carousel_cache():
    from apps.models.content_model import TL_Content_Model
    conList = TL_Content_Model.query.order_by(TL_Content_Model.id.desc()).filter(TL_Content_Model.status == 1, TL_Content_Model.is_roll == 1,TL_Content_Model.update_time < datetime.now()).limit(1000).all()
    return conList

# @cache.cached(timeout=60 * 60, key_prefix='clickRanking_cache')
def clickRanking_cache():
    from apps.models.content_model import TL_Content_Model
    cont = TL_Content_Model.query.order_by(TL_Content_Model.clicks.desc()).filter(TL_Content_Model.update_time < datetime.now()).limit(1000).all()
    return cont

